package com.yisu.expressway.onedollar.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yisu.expressway.R;

/**
 *
 */
public class TitleView extends RelativeLayout {

    public static final String TAG_LEFT_BUTTON = "tag_left_button";
    public static final String TAG_TITLE_TEXTVIEW = "tag_title_textview";
    public static final String TAG_RIGHT_BUTTON = "tag_right_button";
    private Drawable backgroundDrawable;
    private Drawable leftButtonDrawable;
    private Drawable titleDrawable;
    private Drawable rightButtonDrawable;
    private String leftButtonStr;
    private String titleStr;
    private String rightButtonStr;
    private View leftView;
    private TextView leftTv;
    private ImageView leftBtn;
    private TextView titleTv;
    private View rightView;
    private TextView rightTv;
    private ImageView rightBtn;
    private int titleColor;
    private OnTitleBarClickEvent mTitleBarOnClickEvent;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.title, defStyle, 0);

        backgroundDrawable = a.getDrawable(R.styleable.title_backGround);
        leftButtonDrawable = a.getDrawable(R.styleable.title_leftIcon);
        titleDrawable = a.getDrawable(R.styleable.title_titleIcon);
        rightButtonDrawable = a.getDrawable(R.styleable.title_rightIcon);

        leftButtonStr = a.getString(R.styleable.title_leftLabel);
        titleStr = a.getString(R.styleable.title_titleLabel);
        rightButtonStr = a.getString(R.styleable.title_rightLabel);
        titleColor = a.getColor(R.styleable.title_titleColor, getResources().getColor(android.R.color.white));

        LayoutInflater.from(context).inflate(R.layout.view_title_bar, this, true);

        initBackground();

        initLeftButton();

        initTitle();

        initRightButton();
        a.recycle();
    }

    public void setOnTitleBarClickEvent(OnTitleBarClickEvent titleBarOnClickEvent) {
        this.mTitleBarOnClickEvent = titleBarOnClickEvent;
    }

    private void setBounds(Drawable icon) {
        icon.setBounds(0, 0, icon.getMinimumWidth(), icon.getMinimumHeight());
    }

    private void initBackground() {
        if (backgroundDrawable != null) {
            this.setBackgroundDrawable(backgroundDrawable);
        } else {
            this.setBackgroundColor(getResources().getColor(R.color.transparent));
        }


    }


    private void initLeftButton() {
        leftView =  findViewById(R.id.rl_left);
        leftTv = (TextView) findViewById(R.id.tv_left);
        leftBtn = (ImageView) findViewById(R.id.btn_left);
        leftView.setTag(TAG_LEFT_BUTTON);

        if (null != leftButtonDrawable) {
            setIcon(leftBtn, leftButtonDrawable);
        }
        if (!TextUtils.isEmpty(leftButtonStr)) {
            leftTv.setText(leftButtonStr);
        } else {
            leftTv.setVisibility(View.GONE);
        }

        setOnClickEvent(leftView);
    }

    private void initTitle() {
        titleTv = (TextView) findViewById(R.id.tv_title);
        titleTv.setTag(TAG_TITLE_TEXTVIEW);

        if (null != titleDrawable) {
            setTextViewIcon(titleTv, titleDrawable);
            setOnClickEvent(titleTv);
        }

        if (!TextUtils.isEmpty(titleStr)) {
            titleTv.setText(titleStr);
        }
        titleTv.setTextColor(titleColor);
    }

    public void setTitleLongClick(OnLongClickListener onLongClickListener) {
        if (null == onLongClickListener) {
            return;
        }
        if (null == titleTv) {
            return;
        }
        titleTv.setLongClickable(true);
        titleTv.setOnLongClickListener(onLongClickListener);
    }

    public void setLeftIcon(int rsid) {
        if (null == leftBtn) {
            return;
        }
        leftView.setVisibility(View.VISIBLE);
        leftBtn.setVisibility(VISIBLE);
        Drawable drawable = getResources().getDrawable(rsid);
        setIcon(leftBtn, drawable);
        if (!TextUtils.isEmpty(leftButtonStr)) {
            leftTv.setVisibility(VISIBLE);
            leftTv.setText(leftButtonStr);
        } else {
            leftTv.setVisibility(View.GONE);
        }
    }

    public void setRightIcon(int rsid) {
        if (null == rightBtn) {
            return;
        }
        rightView.setVisibility(View.VISIBLE);
        rightBtn.setVisibility(View.VISIBLE);
        Drawable drawable = getResources().getDrawable(rsid);
        setIcon(rightBtn, drawable);
        if (!TextUtils.isEmpty(rightButtonStr)) {
            rightTv.setVisibility(VISIBLE);
            rightTv.setText(leftButtonStr);
        } else {
            rightTv.setVisibility(View.GONE);
        }
    }

    public void setRightLabel(String label) {
        if (null == rightTv) {
            return;
        }
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText(label);
    }

    public void setRightEnable(boolean isEnable) {
        if (null == rightView) {
            return;
        }
        rightView.setEnabled(isEnable);
    }

    public void setRightInvisible() {
        rightView.setVisibility(View.INVISIBLE);
    }

    public void setRightVisible() {
        rightView.setVisibility(View.VISIBLE);
    }

    public void setTitleIcon(int rsid) {
        if (null == titleTv) {
            return;
        }
        Drawable drawable = getResources().getDrawable(rsid);
        setTextViewIcon(titleTv, drawable);
        setOnClickEvent(titleTv);
    }

    public void setTitle(int titleRes) {
        if (null == titleTv) {
            return;
        }
        titleTv.setText(titleRes);
    }

    public void setTitle(String titleStr) {
        if (null == titleTv) {
            return;
        }
        titleTv.setText(titleStr);
    }

    public void setTitle(CharSequence titleChar) {
        if (null == titleChar) {
            return;
        }
        titleTv.setText(titleChar);
    }

    public void setTitleEllipsize(TruncateAt where) {
        if (null == titleTv) {
            return;
        }
        titleTv.setEllipsize(where);
    }

    private void initRightButton() {
        rightView = findViewById(R.id.rl_right);
        rightTv = (TextView) findViewById(R.id.tv_right);
        rightBtn = (ImageView) findViewById(R.id.btn_right);
        rightView.setTag(TAG_RIGHT_BUTTON);

        if (null != rightButtonDrawable) {
            setIcon(rightBtn, rightButtonDrawable);
        }
        if (!TextUtils.isEmpty(rightButtonStr)) {
            rightTv.setText(rightButtonStr);
        } else {
            rightTv.setVisibility(View.INVISIBLE);
        }

        setOnClickEvent(rightView);
    }

    private void setIcon(ImageView imageView, Drawable drawable) {
        setBounds(drawable);
        imageView.setImageDrawable(drawable);
    }
    private void setTextViewIcon(TextView textView, Drawable drawable) {
        setBounds(drawable);
        textView.setCompoundDrawables(null,null,drawable,null);
    }

    private void setOnClickEvent(View view) {
        view.setClickable(true);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null == mTitleBarOnClickEvent) {
                    return;
                }

                String tag = (String) v.getTag();
                if (TAG_LEFT_BUTTON.equals(tag)) {// 左侧按钮
                    mTitleBarOnClickEvent.onLeftClick(v);

                }  else if (TAG_TITLE_TEXTVIEW.equals(tag)) {// 中间title
                    mTitleBarOnClickEvent.onTitleClick(v);

                } else {// 右侧按钮
                    mTitleBarOnClickEvent.onRightClick(v);

                }
            }
        });
    }

    public TextView getRightTv() {
        return rightTv;
    }

    /**
     * @return
     */
    public TextView getLeftTv() {
        return leftTv;
    }

    /**
     *
     */
    public interface OnTitleBarClickEvent {
        /**
         * @param view
         */
        public void onLeftClick(View view);

        /**
         * @param view
         */
        public void onTitleClick(View view);

        /**
         * @param view
         */
        public void onRightClick(View view);
    }

}
